package accel.activities;

import accel.dataholders.Character;
import accel.dataholders.Weapon;
import accel.storage.DBManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LoadCharacterActivity extends Activity{
	
	private TextView wname, str,ener,heal, cha, spe,sdesc, wdesc, sname, str2,ener2,heal2, cha2, spe2, str3,ener3,heal3, cha3, spe3, ls, ls2, ls3, lvl, exp;
	private Gallery portrait;
	private Character [] cs;
	private ImageView wimage;
	private int charId;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}
	
	private void init()
	{
		DBManager db = DBManager.getInstance(this);
		cs = db.loadCharacters();
		if (cs.length == 0)
			this.setContentView(R.layout.nocharacters);
		else
		{
			this.setContentView(R.layout.loadcharacter);
	        
	        str = (TextView)findViewById(R.id.strength);
	        ener = (TextView)findViewById(R.id.energy);
	        heal = (TextView)findViewById(R.id.health);
	        cha = (TextView)findViewById(R.id.charisma);
	        spe = (TextView)findViewById(R.id.speed);
	        ls = (TextView)findViewById(R.id.lspeed);
	        
	        str2 = (TextView)findViewById(R.id.strength2);
	        ener2 = (TextView)findViewById(R.id.energy2);
	        heal2 = (TextView)findViewById(R.id.health2);
	        cha2 = (TextView)findViewById(R.id.charisma2);
	        spe2 = (TextView)findViewById(R.id.speed2);
	        ls2 = (TextView)findViewById(R.id.lspeed2);
	        
	        str3 = (TextView)findViewById(R.id.strength3);
	        ener3 = (TextView)findViewById(R.id.energy3);
	        heal3 = (TextView)findViewById(R.id.health3);
	        cha3 = (TextView)findViewById(R.id.charisma3);
	        spe3 = (TextView)findViewById(R.id.speed3);
	        ls3 = (TextView)findViewById(R.id.lspeed3);
	       
	        lvl = (TextView)findViewById(R.id.level);
	        exp = (TextView)findViewById(R.id.exp);
	        sdesc = (TextView)findViewById(R.id.soulDesc);
	        wdesc = (TextView)findViewById(R.id.weaponDesc);
	        sname = (TextView)findViewById(R.id.soulName);
	        wname = (TextView)findViewById(R.id.weaponName);
	        wimage = (ImageView)findViewById(R.id.weaponImage);
	        portrait = (Gallery)findViewById(R.id.portrait);
	        portrait.setAdapter(new ImageAdapter(this));
	        portrait.setHorizontalFadingEdgeEnabled(true);
	        portrait.setOnItemSelectedListener(new OnItemSelectedListener(){
	        	public void onNothingSelected(AdapterView<?> a){}
	        	
	        	public void onItemSelected(AdapterView<?> adapter, View v, int i, long l)
	        	{
	        		Character soul = cs [i];
	        		Weapon weapon = cs[i].getWeapon();
	        		sname.setText(LoadCharacterActivity.this.getString(soul.getName()));
	        		
	        		str.setText(Integer.toString(soul.getStats().maxstrength));
	        		ener.setText(Integer.toString(soul.getStats().maxenergy));
	        		heal.setText(Integer.toString(soul.getStats().maxhealth));
	        		cha.setText(Integer.toString(soul.getStats().maxcharisma));
	        		spe.setText(Integer.toString(soul.getStats().maxspeed));
	        		ls.setText(Integer.toString(soul.getStats().maxlearningspeed));
	        		
	        		setValues(soul.getStats().maxstrength, weapon.getStats().maxstrength, str2, str3);
	        		setValues(soul.getStats().maxenergy, weapon.getStats().maxenergy, ener2, ener3);
	        		setValues(soul.getStats().maxhealth, weapon.getStats().maxhealth, heal2, heal3);
	        		setValues(soul.getStats().maxcharisma, weapon.getStats().maxcharisma, cha2, cha3);
	        		setValues(soul.getStats().maxspeed, weapon.getStats().maxspeed, spe2, spe3);
	        		setValues(soul.getStats().maxlearningspeed, weapon.getStats().maxlearningspeed, ls2, ls3);
	        		
	        		wimage.setImageResource(weapon.getPortrait());
	        		wname.setText(LoadCharacterActivity.this.getString(weapon.getName()));
	        		
	        		lvl.setText(Integer.toString(soul.getLevel()));
	        		exp.setText(Integer.toString(soul.getExp()) + " / " + Integer.toString(soul.expForNextLevel()));
	        		
	        		sdesc.setText(LoadCharacterActivity.this.getString(soul.getDesciption()));
	        		wdesc.setText(LoadCharacterActivity.this.getString(weapon.getDesciption()));
	        		charId = i;
	        	}
	        	private void setValues(int a, int b, TextView tx1, TextView tx2) //a - soul.stats, b - weapon.stats
	        	{
	        		if (b > 0)
	        		{
	        			tx1.setText("+" + Integer.toString(b));
	        			tx1.setTextColor(Color.GREEN);
	        			tx2.setText(Integer.toString(a + b));
	        			tx2.setBackgroundColor(Color.GREEN);
	        		}
	        		else if (b == 0)
	        		{
	        			tx1.setText("0");
	        			tx1.setTextColor(Color.GRAY);
	        			tx2.setText(Integer.toString(a));
	        			tx2.setBackgroundColor(Color.TRANSPARENT);
	        		}
	        		else
	        		{
	        			tx1.setText(Integer.toString(b));
	        			tx1.setTextColor(Color.RED);
	        			tx2.setText(Integer.toString(a + b));
	        			//str3.setTextColor(Color.RED);
	        			tx2.setBackgroundColor(Color.RED);
	        		}
	        	}
	        });
		}
	}
	
	public class ImageAdapter extends BaseAdapter {
	    int mGalleryItemBackground;
	    private Context mContext;

	    private Integer[] pics;

	    public ImageAdapter(Context c) {
	        mContext = c;
	        pics = new Integer[cs.length];
	        for (int i = 0; i<cs.length; i++)
	        	pics[i] = cs[i].getPortrait();
	    }

	    public int getCount() {
	        return pics.length;
	    }

	    public Object getItem(int position) {
	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }

	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView i = new ImageView(mContext);

	        i.setImageResource(pics[position]);

	        return i;
	    }
	}
	
	public void nextScreen(View v)
	{
		Character.instance = cs[charId];
		startActivity(new Intent(this, CharacterScreenActivity.class));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.load_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId())
		{
		case R.id.delete:
			try
			{
				DBManager.getInstance(this).deleteCharacter(cs[charId]);
			}
			catch(Exception e)
			{
				Toast.makeText(this, "Failed to delete character" , Toast.LENGTH_SHORT).show();
				return true;
			}
			init();
			return true;
		default:
            return super.onOptionsItemSelected(item);
		}
	}

}
